﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace MetroLightKit.Controls
{
    [TemplateVisualState(GroupName = "MetroStates", Name = "NormalState")]
    [TemplateVisualState(GroupName = "MetroStates", Name = "MouseOverState")]
    [TemplateVisualState(GroupName = "MetroStates", Name = "MetroMouseOverState")]
    [TemplateVisualState(GroupName = "MetroStates", Name = "PressedState")]
    [TemplateVisualState(GroupName = "MetroStates", Name = "MetroPressedState")]
    public class MetroButton : Button
    {
        public static readonly DependencyProperty ImageTemplateProperty = DependencyProperty.Register("ImageTemplate",
                                                                                                      typeof (
                                                                                                          DataTemplate),
                                                                                                      typeof (
                                                                                                          MetroButton),
                                                                                                      null);

        public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register("ImageWidth",
                                                                                                   typeof (double),
                                                                                                   typeof (MetroButton),
                                                                                                   null);

        public static readonly DependencyProperty ImageScaleYProperty = DependencyProperty.Register("ImageScaleY",
                                                                                                    typeof (double),
                                                                                                    typeof (MetroButton),
                                                                                                    null);

        public static readonly DependencyProperty ImageScaleXProperty = DependencyProperty.Register("ImageScaleX",
                                                                                                    typeof (double),
                                                                                                    typeof (MetroButton),
                                                                                                    null);

        public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation",
                                                                                                    typeof (Orientation),
                                                                                                    typeof (MetroButton),
                                                                                                    null);

        public static readonly DependencyProperty ContentMarginProperty = DependencyProperty.Register("ContentMargin",
                                                                                                      typeof (Thickness),
                                                                                                      typeof (
                                                                                                          MetroButton),
                                                                                                      null);

        public static readonly DependencyProperty ImageMarginProperty = DependencyProperty.Register("ImageMargin",
                                                                                                    typeof (Thickness),
                                                                                                    typeof (MetroButton),
                                                                                                    null);

        public static readonly DependencyProperty CercleThicknessProperty =
            DependencyProperty.Register("CercleThickness", typeof (double), typeof (MetroButton), null);

        public static readonly DependencyProperty ImageControlTemplateProperty =
            DependencyProperty.Register("ImageControlTemplate", typeof (ControlTemplate), typeof (MetroButton), null);

        public static readonly DependencyProperty ForegroundOverProperty = DependencyProperty.Register(
            "ForegroundOver", typeof (Color), typeof (MetroButton), null);

        public static readonly DependencyProperty ImageStyleProperty = DependencyProperty.Register("ImageStyle",
                                                                                                   typeof (Style),
                                                                                                   typeof (MetroButton),
                                                                                                   null);

        public static readonly DependencyProperty IsMetroBackgroungProperty =
            DependencyProperty.Register("IsMetroBackgroung", typeof (bool), typeof (MetroButton), null);

        public MetroButton()
        {
            IsMetroBackgroung = true;
            base.DefaultStyleKey = typeof (MetroButton);
        }

        public bool IsMetroBackgroung
        {
            get { return (bool) base.GetValue(IsMetroBackgroungProperty); }
            set { base.SetValue(IsMetroBackgroungProperty, value); }
        }

        public Color ForegroundOver
        {
            get { return (Color) base.GetValue(ForegroundOverProperty); }
            set { base.SetValue(ForegroundOverProperty, value); }
        }

        public ControlTemplate ImageControlTemplate
        {
            get { return (ControlTemplate) base.GetValue(ImageControlTemplateProperty); }
            set { base.SetValue(ImageControlTemplateProperty, value); }
        }

        public Style ImageStyle
        {
            get { return (Style) base.GetValue(ImageStyleProperty); }
            set { base.SetValue(ImageStyleProperty, value); }
        }

        public DataTemplate ImageTemplate
        {
            get { return (DataTemplate) base.GetValue(ImageTemplateProperty); }
            set { base.SetValue(ImageTemplateProperty, value); }
        }

        public Orientation Orientation
        {
            get { return (Orientation) base.GetValue(OrientationProperty); }
            set { base.SetValue(OrientationProperty, value); }
        }

        public double ImageWidth
        {
            get { return (double) base.GetValue(ImageWidthProperty); }
            set { base.SetValue(ImageWidthProperty, value); }
        }

        public double ImageScaleX
        {
            get { return (double) base.GetValue(ImageScaleXProperty); }
            set { base.SetValue(ImageScaleXProperty, value); }
        }

        public double ImageScaleY
        {
            get { return (double) base.GetValue(ImageScaleYProperty); }
            set { base.SetValue(ImageScaleYProperty, value); }
        }

        public double CercleThickness
        {
            get { return (double) base.GetValue(CercleThicknessProperty); }
            set { base.SetValue(CercleThicknessProperty, value); }
        }

        public Thickness ContentMargin
        {
            get { return (Thickness) base.GetValue(ContentMarginProperty); }
            set { base.SetValue(ContentMarginProperty, value); }
        }

        public Thickness ImageMargin
        {
            get { return (Thickness) base.GetValue(ImageMarginProperty); }
            set { base.SetValue(ImageMarginProperty, value); }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            VisualStateManager.GoToState(this, IsMetroBackgroung ? "MetroMouseOverState" : "MouseOverState", true);
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            VisualStateManager.GoToState(this, "NormalState", true);
        }

        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            VisualStateManager.GoToState(this, IsMetroBackgroung ? "MetroPressedState" : "PressedState", true);
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonUp(e);
            VisualStateManager.GoToState(this, IsMetroBackgroung ? "MetroMouseOverState" : "MouseOverState", true);
        }
    }
}